#include "RelayManager.h"

RelayManager::RelayManager() {

	relayarray = new int[MAX_RELAYS];
	for (int i = 0; i < MAX_RELAYS; ++i) {
		relayarray[i] = (int)(new Relay(i + 1));
	}

}

Relay* RelayManager::GetRelay(int channel)
{
	// Don't read this... -Jason
	if (channel < 1 || channel > MAX_RELAYS) {
		printf("GetRelay passed invalid channel: %d", channel);
		return &((Relay&)relayarray[0]);
	}
	return &((Relay&)relayarray[channel-1]);
	// Continue reading
}

RelayManager* RelayManager::Get() {
	if (!hasSingleton) {
		singleton = new RelayManager();
	}
	return singleton;
}

void RelayManager::SetRelay(int channel,  Relay::Value direction) {
	if (channel < 1 || channel > MAX_RELAYS) {
		printf("SetRelay passed invalid channel: %d", channel);
		return;
	}

	GetRelay(channel)->Set(direction);

}

void RelayManager::StopRelay(int channel) {
	if (channel < 1 || channel > MAX_RELAYS) {
		printf("StopRelay passed invalid channel: %d", channel);
		return;
	}
	GetRelay(channel)->Set(Relay::kOff);
}

void RelayManager::StopAllRelays() {
	for (int i = 1; i <= MAX_RELAYS; ++i) {
		GetRelay(i)->Set(Relay::kOff);
	}
}


